﻿Imports System
Imports IDServer
Imports System.Threading.Thread
Imports System.IO
Imports System.String
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Frm_SelectQuotePrice
	Implements IDServer.IDServer.IDServer

	Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
	Private mDataset As DataSet
    Private FrmMain As New Frm_MainForm
	Public Fields, strFileName As String
    Private WithEvents MainFrom As New Frm_MainForm
    Private Captionstr As String = "條碼^Barcode&代碼^Code&中文貨品名^Cnname&英文貨品名^Enname&顔色^Color&規格^Spec&單位^Unit&單價^Price&貨品類別^GoodsType&訂貨量^POQty"

    Private _ValidDate As String
    Public Property ValidDate() As String
        Get
            Return _ValidDate
        End Get
        Set(ByVal value As String)
            _ValidDate = value
        End Set
    End Property


    Private Sub Frm_SelectGoods_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Try
            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                If e.KeyCode = Keys.Enter Then
                    SendKeys.Send("+{TAB}")
                    SendKeys.Send("{F4}")
                End If
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        SendKeys.Send("{TAB}")
                End Select
            End If
        Catch ex As Exception
            MsgBox("txtCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub Frm_SelectGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.txtEnname.Focus()
            Me.Cursor = Cursors.WaitCursor

            DataBindGridView(Me.dgrdGoods, Captionstr)
            DataBindGrid(Me.dgrdGoods, Captionstr)
            'BindGridCaption(Me.dgrdCustomer, BindHashTable(Captionstr))
            FormatDataGridView(Me.dgrdGoods)
            With Me.dgrdGoods
                .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                .MultiSelect = PubMultiSelect
            End With


            FillCmb(Me.cmbColor, "ColorDefine", True)
            FillCmb(Me.cmbSpec, "Size", True)

            '' Me.txtBarcode.Focus()




            'listAllControl()

        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            Me.Cursor = Cursors.Default
            Me.txtEnname.Focus()
        End Try
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function


    Private Sub txtBarcode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBarcode.KeyDown, txtCode.KeyDown, _
                                   txtCnname.KeyDown, txtEnname.KeyDown, cmbColor.KeyDown, _
                                    cmbSpec.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        Me.BtnFind_Click(sender, e)
                    Case Keys.Escape
                        Me.btnCancel_Click(sender, e)
                End Select
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtBarcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBarcode.TextChanged, txtCnname.TextChanged, _
               txtCode.TextChanged, txtEnname.TextChanged, cmbColor.TextChanged, cmbSpec.TextChanged
        Me.BtnFind_Click(sender, e)

    End Sub

    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click

        Try
            Dim Sql As String

            Sql = "SELECT barcode = Substring(a.barcode,2,Len(Rtrim(a.barcode)) - 2),"
            Sql = Sql & "        a.code,"
            Sql = Sql & "        a.cnname,"
            Sql = Sql & "        a.enname,"
            Sql = Sql & "        color = e.colorcname,"
            Sql = Sql & "        spec = j.sizesname,"
            Sql = Sql & "        unit = b.lista,"
            Sql = Sql & "        a.purhaseprice,"
            Sql = Sql & "        goodstype = f.ffullname,"
            Sql = Sql & "        a.poqty"
            Sql = Sql & " INTO   #t1"
            Sql = Sql & " FROM   dbo.bsc_goods a"
            Sql = Sql & "        LEFT JOIN (SELECT c.itemcode,"
            Sql = Sql & "                          d.listcode,"
            Sql = Sql & "                          d.lista"
            Sql = Sql & "                   FROM   bsc_otherdefinea c,"
            Sql = Sql & "                          bsc_otherdefineb d"
            Sql = Sql & "                   WHERE  c.itemcode = 'UnitDefin'"
            Sql = Sql & "                          AND c.id = d.itemno) b"
            Sql = Sql & "          ON a.unit = b.listcode"
            Sql = Sql & "        LEFT JOIN bsc_colordefine e"
            Sql = Sql & "          ON a.color = e.colorcode"
            Sql = Sql & "        LEFT JOIN bsc_goodstype f"
            Sql = Sql & "          ON a.goodstype = f.id"
            Sql = Sql & "        LEFT JOIN bsc_storehouse i"
            Sql = Sql & "          ON a.storehouse = i.fcode"
            Sql = Sql & "        LEFT JOIN bsc_sizedefine j"
            Sql = Sql & "          ON a.spec = j.sizecode"
            Sql = Sql & " WHERE  1 = 1"
            Sql = Sql & "        AND a.status = 1  "

            If Me.txtBarcode.TextLength > 0 Then
                Sql += " and a.BarCode like '%" & Me.txtBarcode.Text & "%'"
            End If

            If Me.txtCnname.TextLength > 0 Then
                Sql += " and a.Cnname like '%" & Me.txtCnname.Text & "%'"
            End If

            If Me.txtEnname.TextLength > 0 Then
                Sql += " and a.Enname like '%" & Me.txtEnname.Text & "%'"
            End If

            If Me.txtCode.TextLength > 0 Then
                Sql += " and a.Code like '%" & Me.txtCode.Text & "%'"
            End If

            If Me.cmbColor.Text.Length > 0 Then
                Sql += " and a.Color like '%" & Microsoft.VisualBasic.Left(Me.cmbColor.Text, Me.cmbColor.Text.IndexOf(" ")) & "%'"
            End If

            If Me.cmbSpec.Text.Length > 0 Then
                Sql += " and a.Spec like '%" & Microsoft.VisualBasic.Left(Me.cmbSpec.Text, Me.cmbSpec.Text.IndexOf(" ")) & "%'"
            End If

            Sql += ";"

            Sql += "select a.Num,a.Date,a.ValidDate,a.SuppCode,a.CurrencyID,"
            Sql += " Barcode=substring(b.barcode,2,len(rtrim(b.barcode))-2),b.Price "
            Sql += " INTO #T2 FROM Pur_QuoteA a,Pur_QuoteB b "
            Sql += " WHERE a.Billstatusid='B' and a.Num=b.Num   and a.Suppcode='" & PubSelectPriceSupplier & "'"
            Sql += " and a.ValidDate<='" & Me.ValidDate & "'"
            Sql += "   and a.ValidDate = "
            Sql += "     (select Max(c.ValidDate) FROM Pur_QuoteA c,Pur_QuoteB d "
            Sql += "            WHERE d.Barcode=b.Barcode"
            Sql += "            and c.Suppcode=a.Suppcode"
            Sql += "              and c.Num=d.Num "
            Sql += "            GROUP BY c.SuppCode,d.Barcode)"
            Sql += "  order by b.barcode;"

            Sql += "select a.Barcode,a.Code,a.Cnname,a.Enname,a.Color,a.Spec,a.Unit,"
            Sql += "       Price=(CASE WHEN ISNULL(b.Price,0)=0 THEN a.PurhasePrice ELSE b.Price END),"
            Sql += "       a.GoodsType,a.POQty"
            Sql += " from #t1 a left join #t2 b"
            Sql += " on a.barcode=b.barcode"
            Sql += " order by a.barcode;"

            mDataset = ReturnDataset(Sql)

            Me.dgrdGoods.DataSource = mDataset.Tables(0)

        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            Me.Cursor = Cursors.Default

        End Try

    End Sub


    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try
            If Me.dgrdGoods.SelectedRows.Count > 0 Then
                PubGoodsDataSet.Tables.Clear()

                Dim tb As New DataTable
                Dim FirstSelectRow, LastSelectRow, intSelectRows As Integer
                Dim intRows() As Integer

                '選擇的第1行

                FirstSelectRow = Me.dgrdGoods.SelectedRows(0).Index

                '選擇的最後一行

                If Me.dgrdGoods.SelectedRows.Count > 1 Then
                    LastSelectRow = Me.dgrdGoods.SelectedRows(Me.dgrdGoods.SelectedRows.Count - 1).Index

                End If


                intSelectRows = Me.dgrdGoods.SelectedRows.Count
                ReDim intRows(intSelectRows - 1)
                For i As Integer = 0 To intSelectRows - 1
                    intRows(i) = Me.dgrdGoods.SelectedRows(i).Index
                Next

                tb = mDataset.Tables(0).Copy
                tb.Rows.Clear()

                PubGoodsDataSet.Tables.Add(tb)

                '順向選擇
                If FirstSelectRow > LastSelectRow Then
                    For J As Integer = intRows.LongLength - 1 To 0 Step -1
                        'For Each row As DataGridViewRow In Me.dgrdGoods.Rows

                        Dim mRow As DataRow
                        mRow = tb.NewRow()

                        For Each col As DataColumn In tb.Columns
                            mRow(col) = Me.dgrdGoods.Rows(intRows(J)).Cells(col.ColumnName).Value
                        Next

                        tb.Rows.Add(mRow)
                        PubGoodsDataSet.AcceptChanges()

                        'Next

                    Next
                Else
                    '反向選擇
                    For J As Integer = 0 To intRows.LongLength - 1
                        'For Each row As DataGridViewRow In Me.dgrdGoods.Rows

                        Dim mRow As DataRow
                        mRow = tb.NewRow()

                        For Each col As DataColumn In tb.Columns
                            mRow(col) = Me.dgrdGoods.Rows(intRows(J)).Cells(col.ColumnName).Value
                        Next

                        tb.Rows.Add(mRow)
                        PubGoodsDataSet.AcceptChanges()

                    Next



                End If

            End If

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)

        Finally
            Me.Dispose()


        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Me.Close()
        Me.Dispose()


    End Sub

    Private Sub dgrdGoods_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgrdGoods.CellDoubleClick
        Me.btnOK_Click(sender, e)
    End Sub



    Private Sub dgrdGoods_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgrdGoods.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        Me.btnOK_Click(sender, e)
                    Case Keys.Escape
                        Me.btnCancel_Click(sender, e)
                End Select
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    Private Sub dgrdGoods_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgrdGoods.CellContentClick

    End Sub
End Class